package exer;

/**
 * @author tongchen
 * @create 2023-01-13 19:14
 */
public class MyBlockingQueue {
    //为了避免多线程情况下的读写不一致情况，给变量加volatile
    private volatile int front;
    private volatile int tail;
    private volatile int size;
    private int[]element=new int[1000];
    //加入方法
    public void put(Integer value) throws InterruptedException {
        synchronized (this){

            //判断是否已满
            while(size==element.length){
                System.out.println("当前队列已满");
                this.wait();
            }
            //加入操作
            element[tail]=value;
            tail=(tail+1)% element.length;
            size++;
            this.notifyAll();
        }
    }
    //去除元素操作
    public int remove () throws InterruptedException {
        synchronized(this){

            //判断是否为空
            while(size==0){
                System.out.println("当前队列为空");
                this.wait();
            }
            int value=element[front];
            //删除操作
            front=(front+1)%element.length;
            size--;
            this.notifyAll();
            return value;
        }

    }
}
